<?php
/**
 * Created by PhpStorm.
 * User: lei
 * Date: 15-6-4
 * Time: 下午9:10
 */
use Phalcon\Mvc\Model;
class ServerUsers extends Model
{
    public $id;

    public $user;

    public $name;

    public $password;

    public $phone;

    public $mobile;

    public $email;

    public $addTime;

    public $updateTime;


    public function initialize()
    {   //protected Phalcon\Mvc\Model\Relation hasMany (unknown $fields, unknown $referenceModel, unknown $referencedFields, [unknown $options])
        $this->hasMany('id', 'BusinessUsers', 'businessUserId', array(
            'foreignKey' => array(
                'message' => '业务用户正在使用平台用户，无法进行删除操作！'
            )
        ));
    }

    public function getServerUserByCasUser($casUserId)
    {
        $conditions = " user = :user:";
        $parameters = array("user" => "$casUserId");
        $serverUsers = ServerUsers::findFirst(array($conditions, "bind" => $parameters));
        return $serverUsers;
    }

    /**
     *
     *
     * @param $casUserId
     * @return mixed
     */
    public function getBusinessSystemsByCasUser($casUserId)
    {
        // @todo 查询待优化
        $serverUserId = $this->getServerUserByCasUser($casUserId);
        $conditions = " serverUserId = :serverUserId:";
        $parameters = array("serverUserId" => "$serverUserId->id");
        $businessUser = BusinessUsers::find(array($conditions, "bind" => $parameters));
        return $businessUser;
    }

}